﻿using System.Collections;
using UnityEngine;
using QFramework;
using cfg;
using Game.Assets.Scripts.Manager;
using UnityEngine.U2D;
using Game.CheseBoard;
using System.Collections.Generic;

namespace Game.Util
{
    public class AssetUtil : IUtility, IController
    {
        public IArchitecture GetArchitecture()
        {
            return APP.Interface;
        }

        TableManager table;
        public TableManager Table
        {
            get
            {
                if (table == null)
                {
                    table = this.GetModel<TableManager>();
                }
                return table;
            }
        }
        cfg.Table.TbImage tbImage;
        /// <summary>
        /// 获取棋子图标
        /// </summary>
        /// <param name="piece"></param>
        /// <returns></returns>
        public Sprite GetPieceSprite(Piece piece)
        {
            if (tbImage == null)
                tbImage = Table.Tables.TbImage;

            if (!tbImage.DataMap.ContainsKey(piece.PiecePrefab))
            {
                return null;
            }
            cfg.Image image = tbImage.Get(piece.PiecePrefab);
            ResLoader loader = ResLoader.Allocate();
            return loader.LoadSync<SpriteAtlas>(image.ImageAtlas).GetSprite(image.ImagePrefab);
        }

        readonly Dictionary<int, string> extraItemDic = new Dictionary<int, string>()
        {
            {1,"TimeAccelerator" },
            {2,"InfinitePower" },
            {3,"Splitter" },
            {4,"SingleTimeAccelerator" },
            {5,"NearbyTimeAccelerator" },
            {6,"UpgradeCard" },
            {7,"CurrencyCell" },
            {8,"BagCell" },
            {9,"MapCell" },
            {10,"ReplenishCell" },
        };



        /// <summary>
        /// 获取棋子预设体
        /// </summary>
        /// <param name="piece"></param>
        /// <returns></returns>
        public Cell GetPiecePrefab(Piece piece)
        {
            ResLoader loader = ResLoader.Allocate();
            switch ((PieceType)piece.PieceType)
            {
                case PieceType.Ordinary:
                    return loader.LoadSync<Cell>("Piece");
                case PieceType.Producer:
                    return loader.LoadSync<Cell>("Producer");
                case PieceType.Soldier:
                    return loader.LoadSync<Cell>("Piece");
                case PieceType.ExtraItem:
                    return loader.LoadSync<Cell>(extraItemDic[Table.Tables.TbExtraItem.Get(piece.PieceId).ExtraItemType]);
                default:
                    return null;
            }
        }

        /// <summary>
        /// 获取棋子预设体
        /// </summary>
        /// <param name="piece"></param>
        /// <returns></returns>
        public GameObject GetCheeseSelectPrefab()
        {
            ResLoader loader = ResLoader.Allocate();
            return loader.LoadSync<GameObject>("CheeseSelect");
        }

    }
}